import sys

from PyQt4.QtCore import *
from PyQt4.QtGui import *

from arduino import Arduino

__version__ = "0.0.1"

UPDATE_RATE = 2 # seconds one update

class ArduinoGUI(QDialog):
    
    def __init__(self, parent=None):
        super(ArduinoGUI, self).__init__(parent, Qt.WindowMinMaxButtonsHint)        

        self.ar = Arduino()
        self.timer = QTimer()
        self.timer.setInterval(UPDATE_RATE * 1000)

        self.lbl_temp = QLabel("Temp:")
        self.lcd_temp = QLCDNumber()
        self.lbl_humi = QLabel("Humi:")
        self.lcd_humi = QLCDNumber()
        self.btn_on = QPushButton("On")
        self.btn_off = QPushButton("Off")

        layout = QVBoxLayout()
        layout.addWidget(self.lcd_temp)
        layout.addWidget(self.lcd_humi)
        layout.addWidget(self.btn_on)
        layout.addWidget(self.btn_off)
        self.setLayout(layout)

        self.connect(self.btn_on, SIGNAL("clicked()"), self.switch_on)
        self.connect(self.btn_off, SIGNAL("clicked()"), self.switch_off)
        self.connect(self.timer, SIGNAL("timeout()"), self.show_temp_humi)

        self.timer.start()

        self.setFixedSize(300, 300)        
        self.setWindowTitle("ArduinoGUI %s" % __version__)

    def switch_on(self):
        self.ar.turn_on_switch()

    def switch_off(self):
        self.ar.turn_off_switch()

    def show_temp_humi(self):
        hum_temp = self.ar.get_hum_temp().split()
        self.lcd_humi.display(hum_temp[0][:-1])
        self.lcd_temp.display(hum_temp[1][:-1])
        

if __name__=="__main__":
    app = QApplication(sys.argv)
    form = ArduinoGUI()
    form.show()
    sys.exit(app.exec_())
